//
//  NetworkUtil.m
//  NiuBanJin
//
//  Created by Billy on 15/9/24.
//  Copyright (c) 2015年 zzjr. All rights reserved.
//

#import "NetworkUtil.h"
#import "Reachability.h"
#import "Ticket.h"
#import "AllMemberTicketExchangeHistory.h"
#import "ScoreAndExchange.h"
#import "AssetList.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface NetworkUtil ()

@property (nonatomic , strong) NSMutableArray *mutableArray_Data;
@property (nonatomic, strong) AFHTTPRequestOperationManager *manager; //暴露在.h文件的原因是可以在调用 的页面取消访问
@end

static NSString * const URLString = @"https://api.app.net/";

@implementation NetworkUtil

+ (NetworkUtil *) sharedNetworkUtil {
    
    static NetworkUtil * networkUtil = nil ;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        networkUtil =  [[self alloc] init];
    });
    
    return  networkUtil;
}

#pragma mark - network access methods list

/**
 *      登陆模块
 *
 */

- (void) zzjrUserTypeCheckingByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_userChecking) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"验证用户是否存在得到的返回值:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
        NSLog(@"ERROR:%@",error);
    }];
}

- (void)zzjrGetSMSAuthenticationCodeByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Login_get_authentication_code) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"注册获取验证码:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void) zzjrSMSAuthenticationCodeComparisonByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_get_authentication_code_verify_result) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"验证码比对:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void) zzjrUserRegisterByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_user_register) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"用户注册:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void) zzjrUserLoginByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_user_login) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"用户登录:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrUserForgetGesturePasswordByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_forget_gesture_password) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"老用户忘记密码:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrUserResetGesturePasswordByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_reset_gesture_password) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"老用户重围密码:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrGetUserAssetsByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Login_user_asset) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"用户登陆完成后资金显示:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}


/**
 *      加息券模块
 *
 */

- (void)zzjrGetTicketslistByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_uservoucherlist) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"用户加息券列表:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer([self parseTicketslistUsingOriginalData:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        transfer(nil,error);
    }];
}

// 解析加息券

- (NSMutableDictionary *) parseTicketslistUsingOriginalData:(NSDictionary*) dic {
    
    NSMutableDictionary *dic_tmp = [NSMutableDictionary dictionary]; // 返回的数据字典
    
    if ([dic[@"status"] integerValue] == 0) {
        
        // 当前利率
        NSDictionary *dic_rateData = dic[@"result"][@"currentRateVO" ];
        
        if (dic_rateData) {
            
            NSString *baseRate = dic_rateData[@"annualRate"];
            NSString *expiredDate = dic_rateData[@"expiredDate"];
            NSString *addRate = dic_rateData[@"ticketRate"];
            
            if (baseRate) {
                
                [dic_tmp setObject:baseRate forKey:@"baseRate"];
            }
            
            if (expiredDate) {
                
                [dic_tmp setObject:expiredDate forKey:@"expiredDate"];
            }
            
            if (addRate) {
                
                [dic_tmp setObject:addRate forKey:@"ticketRate"];
            }
        }
        
        // 加息券列表
        
        NSMutableArray *array_tmp = [NSMutableArray array];
        
        if (dic[@"result"][@"couponList"]) {
            
            for (NSDictionary *dic_list in dic[@"result"][@"couponList"]) {
                
                if (dic_list) {
                    
                    Ticket *tmp = [[Ticket alloc] initWithAttributes:dic_list];
                    [array_tmp addObject:tmp];
                }
            }
        }
        
        [dic_tmp setObject:array_tmp forKey:@"ticketList"];
    } else {
        
        [dic_tmp setObject:[NSMutableArray array] forKey:@"ticketList"];
        //        [UsefulUtil showAlertView:dic[@"msg"]]; //服务器出错的信息
    }
    
    return  dic_tmp;
}

- (void)zzjrUseTicketByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_useTicket) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"使用加息券:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrGetExchangeTicketListByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_tikcetExchangeList) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"可兑换加息券列表:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseExchangeTicketslistByDic:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

//解析可兑换的加息券

- (NSMutableArray *) parseExchangeTicketslistByDic:(NSDictionary *) dic {
    
    NSMutableArray *array_tmp = [NSMutableArray array];
    
    if ([dic count] > 0) {
        
        for (NSDictionary *dic_tmp in dic[@"result"][@"productList"]) {
            
            Ticket *tmp = [[Ticket alloc] initExchangeTicketWithAttributes:dic_tmp];
            [array_tmp addObject:tmp];
        }
    }
    
    return  array_tmp;
}

- (void)zzjrUseExchangeTicketByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_useTicketExchange) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"兑换加息券方法:%@==%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrGetTicketExchangeHistoryByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_memberExchange) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"系统中其它用户兑换的列表:%@",dic);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer([self parseTicketsExchangeListByDic:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (NSMutableArray *) parseTicketsExchangeListByDic:(NSDictionary *) dic {
    
    NSMutableArray *mutableArray_tmp = [NSMutableArray array];
    
    if ([dic count] > 0) {
        
        if ([dic[@"status"] integerValue] == 0 && ([dic[@"result"] count] != 0)) {
            
            for (NSDictionary *dic_element in dic[@"result"]) {
                
                AllMemberTicketExchangeHistory *tmp = [[AllMemberTicketExchangeHistory alloc] initWithAttributes:dic_element];
                [mutableArray_tmp addObject:tmp];
            }
        }else {
            
            NSLog(@"服务器数据是空!");
        }
    }
    
    return  mutableArray_tmp;
}

-(void)zzjrscorechangedByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_scorechanged) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"兑换记录:%@",dic);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseTicketsExchangeHistoryByDictionary:dic],nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (NSMutableArray *) parseTicketsExchangeHistoryByDictionary:(NSDictionary *) dic {
    
    NSMutableArray *mutableArray_tmp = [NSMutableArray array];
    
    if ([[dic objectForKey:@"status"] integerValue ] == 0) {
        
        NSArray *array_result = [dic[@"result"] objectForKey:@"exchangeList"];
        
        for (NSDictionary *dic_exchangeList in array_result) {
            ScoreAndExchange *scoreAndExchange = [[ScoreAndExchange alloc]initWithDictionary:dic_exchangeList];
            
            [mutableArray_tmp addObject:scoreAndExchange];
        }
    }
    
    return mutableArray_tmp;
}

- (void)zzjrRateDetailsByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Coupon_rateDetail) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"兑换记录:%@",dic);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        transfer(nil,error);
    }];
}

//签到
-(void)zzjrScore_usersignByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Score_usersign) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"签到:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

-(void)zzjrScore_getgainlistbyuserByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Score_getgainlistbyuser) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"获得积分的列表:%@",dic);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseAllScoreListByDictionary:dic],nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrScore_getcostlistbyuserByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Score_getcostlistbyuser) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"消耗积分的列表:%@",dic);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseAllScoreListByDictionary:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

-(void)zzjrScore_getscorebyuserByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Score_getscorebyuser) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"全部积分信息的列表:%@",dic);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseAllScoreListByDictionary:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

// 解析 全部积分

- (NSMutableArray *) parseAllScoreListByDictionary:(NSDictionary *) dic {
    
    NSMutableArray *mutableArray_tmp = [NSMutableArray array];
    
    if ([dic count] > 0) {
        
        if ([dic[@"status"] integerValue] == 0) {
            
            for (NSDictionary * dic_element in dic[@"result"]) {
                
                ScoreAndExchange * tmp = [[ScoreAndExchange alloc] initScoreListWithDictionary:dic_element];
                [mutableArray_tmp addObject:tmp];
            }
        }
    }
    
    return  mutableArray_tmp;
}



/**
 *      牛钱包模块
 *
 */

- (void)zzjrUserRealNameAuthenticationByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Member_userAuthentication) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        NSLog(@"实名认证方法:%@=%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrUserIDCardBindigByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_trade_order) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"绑定存入访问server:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrNotifyServerSavingSUccessByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_boughtSuccess) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"存钱成功后访问server:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrUserAssetsListByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_userAssetsList) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"用户资产明细:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]):transfer([self parseAssetListsByDictionary:dic],nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}


- (NSMutableArray *) parseAssetListsByDictionary:(NSDictionary *) dic {
    
    NSMutableArray *mutableArray_tmp = [NSMutableArray array];
    
    if ([dic[@"status" ] intValue] == 0) {
        
        for (NSDictionary *dic_element in dic[@"result"]) {
            
            AssetList *tmp = [[AssetList alloc] initWithAttributes:dic_element];
            
            [mutableArray_tmp addObject:tmp];
        }
    }
    
    return mutableArray_tmp;
}

- (void)zzjrBankListByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_bankLlist) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"银行列表:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

-(void)zzjrTrade_transfer_to_withdrawByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Trade_transfer_to_withdraw) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"转出(提现)的返回值:%@=%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

-(void)zzjrTrade_get_cityByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Trade_get_city) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"根据省份获取市区的返回值:%@=%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

-(void)zzjrTrade_transfer_get_countyByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Trade_transfer_get_county) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"根据城市获取县的返回值:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrBankQueryByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_bank_query) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"用户绑定的银行卡信息返回值:%@=%@",dic,dic[@"msg"]);
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrTradeFeeByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    
    [self.manager POST:URLCOMBINE(Trade_withDraw_fee) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"用户转出手续费返回值:%@=%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
}

- (void)zzjrTrade_real_withDrawByParmeteres:(NSDictionary *) paramsDic transferResultUsingBlock:(transferWebData) transfer {
    [self.manager POST:URLCOMBINE(Trade_withDraw_checking) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"用户转出钱返回值:%@=%@",dic,dic[@"msg"]);
        
        ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
    
}

#pragma mark - AFnetworking manager getter

- (AFHTTPRequestOperationManager *) manager {
    
    if (!_manager) {
        
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        _manager.requestSerializer.timeoutInterval = 60;
        
        [_manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"MOBILE_DEVICE"];
        [_manager.requestSerializer setValue:@"1.0.1" forHTTPHeaderField:@"CLIENT_VERSION"];
        [_manager.requestSerializer setValue:@"appstore" forHTTPHeaderField:@"CHANNEL_NAME"];
    }
    
    return _manager;
}

- (AFHTTPRequestOperationManager *)getManager {
    
    return self.manager;
}

//检测网络状态
-(BOOL)stateOfNetworking{
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    BOOL hasNet = YES;
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:
            hasNet = NO;
            NSLog(@" 没有网络连接");
            break;
        case ReachableViaWWAN:
            hasNet = YES;
            
            NSLog(@" 使用3G网络");
            break;
        case ReachableViaWiFi:
            hasNet = YES;
            NSLog(@"使用WiFi网络");
            break;
            
    }
    return hasNet;
}

// 现阶段2B的解决TOKEN失效的方法
- (void) tokenExpired:(NSString *) msg {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [[UserManager sharedUserManager] logOut];
        });
        
        AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];
        
        UINavigationController *nav_token_expired = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        
        delegate.window.rootViewController = nav_token_expired;
    }
}

@end
